<?php
App::uses('TwitterComponent', 'Controller/Component');

Class ContentsController extends AppController{

	public $helpers = array('Form', 'Html', 'Js'=>array('Jquery'));
	public $components = array('RequestHandler','Rss', 'Twitter');
	public $theme = 'Contents';
	public $uses = array('ArticlesFactory');

		
	public function index(){
		//fetch Twitter
		$publicTimeLine = $this->Twitter->getPublicTimeline('rss');

		//fecth news
		$url = "http://rss.rssad.jp/rss/itmatmarkit/rss.xml";//it's RSS 2.0
		$rss = $this->Rss->parse($url);
		$news = $this->ArticlesFactory->create($rss);	
		//Top Articles
		$topArticles = array();
		for($i=0; $i<2; $i++){
			$topArticles[] = $news[$i];
		}
		//Related Articles
		$relatedArticles = array();
		for($i=2; $i<4; $i++){
			$relatedArticles[] = $news[$i];
		}
		$this->set(compact('topArticles', 'relatedArticles'));
	}
	public function opinion(){
		//fecth News
		$url = "http://rss.rssad.jp/rss/itmatmarkit/rss.xml";//it's RSS 2.0
		$rss = $this->Rss->parse($url);
		$news = $this->ArticlesFactory->create($rss);	
		
		//Top Articles
		$topArticles = array();
		for($i=0; $i<2; $i++){
			$topArticles[] = $news[$i];
		}
		//Related Articles
		$relatedArticles = array();
		for($i=2; $i<4; $i++){
			$relatedArticles[] = $news[$i];
		}

		$word=$this->params['url']['word'];

		$mostPositive = array(
			'clusterId' => 1,
			'id' => 1,
			'description' => 'it is good',
		);
		$mostNegative = array(
			'clusterId' => 1,
			'id' => 1,
			'description' => 'it is bad',
		);
		$this->set(compact('mostPositive', 'mostNegative', 'word','topArticles', 'relatedArticles'));
	}

	public function opinions(){
		//get query parameter
		$word=$this->params['url']['word'];
		//fetch Twitter
		$options = array('q'=>"$word");
		$tweet = $this->Twitter->search($options,'rss');
		
		$goodOpinions =array();
		$goodOpinions[] = $tweet['rss']['channel']['item'][0];
			
		$badOpinions =array();
		$badOpinions[] = $tweet['rss']['channel']['item'][1];

		//fecth News
		$url = "http://rss.rssad.jp/rss/itmatmarkit/rss.xml";//it's RSS 2.0
		$rss = $this->Rss->parse($url);
		$news = $this->ArticlesFactory->create($rss);	
				//Top Articles
		$topArticles = array();
		for($i=0; $i<2; $i++){
			$topArticles[] = $news[$i];
		}
		//Related Articles
		$relatedArticles = array();
		for($i=2; $i<4; $i++){
			$relatedArticles[] = $news[$i];
		}

		$this->set(compact('word', 'goodOpinions', 'badOpinions','topArticles', 'relatedArticles')); 
	}

}




?>
